# -*- coding: utf-8 -*-

import urllib
import re
import json

from base import mcMovie
from base import mcSource

class pelispediaMovie(mcMovie):

	def __init__ (self, id=None):
		mcMovie.__init__(self, "pelispedia", id)


	def loadInfo(self):

		self.url = "http://www.pelispedia.com/movies/play/" + self.id + "/"		
		
		salida = self._mcData.get( self.url )
		salida = salida.replace('\n', ' ')

		#Cover #Title		
		rec_cover_url = re.compile('/movies/box/([^"]*)" alt="([^"]*)')
		match = re.search(rec_cover_url, salida )
		if match != None:
			self.cover_url = match.group(1)
			self.title = unicode(match.group(2).strip(), 'utf-8')

		"""
		#Title
		rec_title = re.compile('"tit">([^<]*)')
		match = re.search(rec_title, salida )
		if match != None:
			self.title = unicode(match.group(1).strip(), 'utf-8')
		"""
		
		#Desc
		rec_description = re.compile('<div class="plot large">([^<]*)')
		match = re.search(rec_description, salida )
		if match != None:
			self.description = unicode(match.group(1).strip(), 'utf-8')

		#Original Title
		rec_original_title = re.compile('<b>Título original:</b>([^<]*)')
		match = re.search(rec_original_title, salida )
		if match != None:
			self.original_title = unicode(match.group(1).strip(), 'utf-8')


		#Idiomas	
		langs = []
		langs.append( "EN" )
		self.languages = langs


		#Subtitles
		langs = []
		langs.append( "ES" )
		self.sub_languages = langs

		#Url
		rec_url = re.compile('data-href="href=([^"]*)')
		match = re.search(rec_url, salida )
		if match != None:
			self.url = unicode(match.group(1).strip(), 'utf-8')
		
		#Parent Call
		mcMovie.loadInfo(self)


	def loadSources(self):
		
		salida = self._mcData.get( self.url )		
		
		#Megaupload
		rec_mega = re.compile('var mega1 = "([^"]*)')
		match = re.search(rec_mega, salida )
		if match != None:
			s = mcSource()
			s.service = self.service
			s.id = self.id
			s.server = 'megaupload'
			s.definition = 360			
			s.is_hd = False			
			s.audio_lang_id = 1
			s.audio_lang = "EN"		
			s.url = "http://www.megaupload.com/?d=" + match.group(1).strip()
			self.sources.append(s)
	
		#bitshare
		rec_mega = re.compile('var mega2 = "([^"]*)')
		match = re.search(rec_mega, salida )
		if match != None:
			s = mcSource()
			s.service = self.service
			s.id = self.id
			s.server = 'bitshare'
			s.definition = 360			
			s.is_hd = False			
			s.audio_lang_id = 1
			s.audio_lang = "EN"		
			s.url = "http://www.bitshare.com/?f=" + match.group(1).strip()
			self.sources.append(s)	
	
	
		#wupload
		rec_mega = re.compile('var mega5 = "([^"]*)')
		match = re.search(rec_mega, salida )
		if match != None:
			s = mcSource()
			s.service = self.service
			s.id = self.id
			s.server = 'wupload'
			s.definition = 360
			s.is_hd = False			
			s.audio_lang_id = 1
			s.audio_lang = "EN"		
			s.url = "http://www.wupload.com/file/" + match.group(1).strip() + "/"
			self.sources.append(s)


		#uploadhere
		rec_mega = re.compile('var mega8 = "([^"]*)')
		match = re.search(rec_mega, salida )
		if match != None:
			s = mcSource()
			s.service = self.service
			s.id = self.id
			s.server = 'uploadhere'
			s.definition = 360
			s.is_hd = False			
			s.audio_lang_id = 1
			s.audio_lang = "EN"		
			s.url = "http://www.uploadhere.com/" + match.group(1).strip()
			self.sources.append(s)		



		#uploadking
		rec_mega = re.compile('var mega9 = "([^"]*)')
		match = re.search(rec_mega, salida )
		if match != None:
			s = mcSource()
			s.service = self.service
			s.id = self.id
			s.server = 'uploadking'
			s.definition = 360
			s.is_hd = False			
			s.audio_lang_id = 1
			s.audio_lang = "EN"		
			s.url = "http://www.uploadking.com/" + match.group(1).strip()
			self.sources.append(s)	

		#Parent Call
		mcMovie.loadSources(self)
	
		#Sub
		#"dsub" href="([^"]*)

	@staticmethod
	def featured():

		o = pelispediaMovie()

		url = "http://www.cuevana.tv/web/inicio"

		salida = o._mcData.get( url )

		result = []

		rec_feat = re.compile('trailer-entry">[^<]*<a href="http://www.pelispedia.com/movies/play/([^/]*)')
		match = re.search(rec_feat, salida )
		if match != None:
			o = pelispediaMovie( unicode( match.group(1).strip() ).encode("utf-8") )
			result.append(o)		

		return result